package com.zhiyou.crud.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.crud.bean.Comment;
import com.zhiyou.crud.bean.Donate;
import com.zhiyou.crud.bean.Project;
import com.zhiyou.crud.bean.Type;
import com.zhiyou.crud.service.ProjectService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 王冲冲
 * @version 1.0
 */
@Controller
public class ProjectController {
    @Resource
    ProjectService projectService;

    //查询所有的项目
    @RequestMapping("/projectManagement/getAll")
    public String getAllProject(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        System.out.println("进入了查询所有");
        //引入pageHelper
        PageHelper.startPage(pn, 5);
        List<Project> projects = projectService.getAllProject();
        List<Type> types = projectService.getAllType();
        PageInfo page = new PageInfo(projects, 5);
        model.addAttribute("pageInfo", page);
        model.addAttribute("type", types);
        return "/admin/products";

    }


    //（项目名称、项目发起人、项目状态、项目类型）检索项目
    @RequestMapping("/projectManagement/getPro")
    public String getOneProject(Project project,Model model) {
        System.out.println("sssssssss+++++++++++++++++++++KKKKKKKKKKKKKKkkkkkkkkkkkkkkkkkkkkkkkkkklllllllllll");
        List<Project> Projects = projectService.getProject(project);
        List<Type> types = projectService.getAllType();
        for (int i = 0; i < Projects.size(); i++) {
            System.out.println(Projects.get(i).getPtitle());
        }
        model.addAttribute("sss", Projects);
        model.addAttribute("typee", types);
        return "/admin/product/sousuo";
    }


    //通过pno查看申请人信息

    @RequestMapping("/projectManagement/getPer")
    public String getPer( Integer pno, Model model) {

        Project project = projectService.getPerByPno(pno);
        model.addAttribute("pro1", project);
        model.addAttribute("comments", project.getComments());
        model.addAttribute("donates", project.getDonates());
        return "/admin/product/product-info1";
    }

    //通过pno查看项目信息
    @RequestMapping("/projectManagement/getProByPno/{pno}")
    public String getProByPno(@PathVariable("pno") Integer pno, Model model) {

        Project project = projectService.getProByPno(pno);
        System.out.println(project.getTname());
        model.addAttribute("pro1", project);

        return "success";
    }


    //通过pno查看项目评论
    @RequestMapping("/projectManagement/getComment/{pno}")
    public String getCommentByPno(@PathVariable("pno") Integer pno, @RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        PageHelper.startPage(pn, 5);
        List<Comment> comments = projectService.getCommentByPno(pno);
        for (int i = 0; i < comments.size(); i++) {
            System.out.println(comments.get(i).getCcontent());
        }
        PageInfo page = new PageInfo(comments, 5);
        model.addAttribute("pageInfo", page);
        return "success";
    }


    //查看项目进度
    @RequestMapping("/projectManagement/getJinDu/{pno}")
    public String getJinDu(@PathVariable("pno") Integer pno) {
        Project project = projectService.getJinDu(pno);
        List<Donate> donates = project.getDonates();
        for (int i = 0; i < donates.size(); i++) {
            System.out.println(donates.get(i).getDmoney());
        }
        return "success";
    }

    //查看所有未审核的项目
    @RequestMapping("/projectManagement/getAllNoPro")
    public String getAllNoPro(Model model) {
        List<Project> projects = projectService.getAllNoPro();

        for (int i = 0; i < projects.size(); i++) {
            System.out.println(projects.get(i).getPtitle());
        }

        model.addAttribute("verify", projects);

        return "/admin/product/shenhe";
    }


    //根据主键审核项目通过且设置开始的时间
    @RequestMapping("/projectManagement/pass")
    public String passProjexct(Project project) {
        System.out.println(project);
        int i = projectService.passProject(project);
        System.out.println(i);
        return "/admin/product/shenhe";
    }

    //根据主键不通过项目
    @RequestMapping("/projectManagement/fail")
    public String failProjexct(Project project) {
        int i = projectService.failProject(project);
        return "/admin/product/shenhe";
    }


    //查看所有项目评论
    @RequestMapping("/projectManagement/getAllComment")
    public String getAllCommect(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        PageHelper.startPage(pn, 5);
        List<Comment> comments = projectService.getAllComment();

        for (int i = 0; i < comments.size(); i++) {
            System.out.println(comments.get(i).getUser().getUname());
        }
        PageInfo page = new PageInfo(comments, 5);
        model.addAttribute("allcomments", page);
        return "/admin/product/Comment";
    }

    //删除某个评论
    @RequestMapping("/projectManagement/deleteOneComment")
    public String deleteOneComment(Integer cono) {
        int i = projectService.deleteOneComment(cono);

        return "/admin/product/Comment";
    }

    /**
     * 对项目分类的操作
     */
    //新增一个项目类型
    @RequestMapping("/projectManagement/addOneType")
    public String addOneType(Type type) {
        int i = projectService.addOneType(type);
        System.out.println("======================================================");
        return "/admin/product/type";
    }

    //删除一个项目类型
    @RequestMapping("/projectManagement/deleteOneType")
    public String deleteOne( Integer tno) {
        int i = projectService.deleteOneType(tno);
        return "/admin/product/type";

    }


    //展示项目类型
    @RequestMapping("/projectManagement/getAllType")
    public String getAllType(Model model) {

        List<Type> types = projectService.getAllType();
        model.addAttribute("types", types);
        return "/admin/product/type";
    }

    //修改某个项目类型
    @RequestMapping("/projectManagement/updateOneType")
    public String updateOneType(Type type) {
        System.out.println("+++++++++++++++++++++++++++++++++++");
        int i = projectService.updateOneType(type);
        return "/admin/product/type";
    }


}
